import sys
parent_dir = "../pygl"
sys.path.append(parent_dir)
import OpenGL.GL as gl
import imgui
import numpy as np
from common.app_v1 import App

class PointApp(App):
    def setupVertex(self) -> None:
        vertex_shader_code = """
        #version 450 core
        void main()
        {
            gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
        }
        """
        fragment_shader_code = """
        #version 450 core
        out vec4 fragment_color;
        void main()
        {
            fragment_color = vec4(0.0, 0.0, 1.0, 1.0);
        }
        """    
        vertex_shader = gl.glCreateShader(gl.GL_VERTEX_SHADER)
        gl.glShaderSource(vertex_shader, vertex_shader_code)
        gl.glCompileShader(vertex_shader)
        fragment_shader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER)
        gl.glShaderSource(fragment_shader, fragment_shader_code)
        gl.glCompileShader(fragment_shader)
        self.program = gl.glCreateProgram()
        gl.glAttachShader(self.program, vertex_shader)
        gl.glAttachShader(self.program, fragment_shader)
        gl.glLinkProgram(self.program)
        gl.glDeleteShader(vertex_shader)
        gl.glDeleteShader(fragment_shader)
        self.vao = np.empty(1, dtype=gl.GLuint)
        gl.glCreateVertexArrays(1, self.vao)
        self.point_size = 20.0
    
    def render(self) -> None:
        gl.glClearNamedFramebufferfv(0, gl.GL_COLOR, 0, self.background_color.to_tuple())
        gl.glUseProgram(self.program)
        gl.glBindVertexArray(self.vao[0])
        gl.glPointSize(self.point_size)
        gl.glDrawArrays(gl.GL_POINTS, 0, 1)
    
    def showImGui(self) -> None:
        imgui.begin("改变点的大小")
        _, new_size = imgui.slider_float("点的大小", self.point_size, 2.0, 80.0)
        self.point_size = new_size
        imgui.end()
    
    def cleanup(self) -> None:
        gl.glDeleteVertexArrays(1, self.vao[0])
        gl.glDeleteProgram(self.program)

if __name__ == "__main__":
    app = PointApp(100, 100, 800, 600, "你好，小不点App！")
    app.run()   

    